﻿Imports System.Data
Partial Class CtrlUsr_Ctrl_Personas
    Inherits CtrlUsrComun

    Public Event SelClicked As EventHandler
    Public Property Tipo() As String
        Get
            Return ViewState("Tipo")
        End Get
        Set(ByVal value As String)
            ViewState("Tipo") = value
        End Set
    End Property
    Public ReadOnly Property Clase() As String
        Get
            Return gvConsulta.SelectedRow.Cells(0).Text
        End Get
    End Property
    Public ReadOnly Property Nombre() As String
        Get
            Return gvConsulta.SelectedRow.Cells(2).Text
        End Get
    End Property
    Public ReadOnly Property Identificacion() As String
        Get
            Return gvConsulta.SelectedRow.Cells(1).Text
        End Get
    End Property

    Public Property Transaccion() As String
        Get
            Return hfTransaccion.Value
        End Get
        Set(ByVal value As String)
            hfTransaccion.Value = Transaccion
        End Set
    End Property
    Public Property IdPk1() As String
        Get
            Return hfPk1.Value
        End Get
        Set(ByVal value As String)
            hfPk1.Value = value
        End Set
    End Property
    Public ReadOnly Property Refrescar() As String
        Get
            Me.gvConsulta.DataBind()
            Return ""
        End Get
    End Property

    Protected Sub btGuardar_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btGuardar.Click
        Dim obj As New Personas
        obj.CEDULA = Me.TxtCed.Text
        obj.TIP_DOC = Me.CmbTipDoc.SelectedValue
        obj.PRIMER_NOMBRE = Me.TxtNom1.Text
        obj.SEGUNDO_NOMBRE = Me.TxtNom2.Text
        obj.PRIMER_APELLIDO = Me.TxtApe1.Text
        obj.SEGUNDO_APELLIDO = Me.TxtApe2.Text
        obj.DIRECCION = Me.TxtDir.Text
        obj.TELEFONO = Me.TxtTel.Text
        obj.CELULAR = Me.TxtCel.Text
        obj.EMAIL = Me.TxtEma.Text
        obj.SEXO = Me.CmbSexo.SelectedValue
        obj.FECHA_NACIMIENTO = Me.DtpFecNac.Text
        obj.IDPK = hfPk1.Value
        Select Case hfTransaccion.Value
            Case "N"
                obj.Insert()
            Case "U"
                obj.Update()
        End Select
        msg.Text = obj.Msg
        MsgBox(msg, obj.lErrorG)
        gvConsulta.DataBind()
        MultiView1.ActiveViewIndex = 1
    End Sub

    Protected Sub btNuevo_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btNuevo.Click
        hfTransaccion.Value = "N"
        limpiar()
        MultiView1.ActiveViewIndex = 0
        lbEstado.Text = "Creando ...."
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            MultiView1.ActiveViewIndex = 1
        End If
    End Sub

    Protected Sub btCancelar_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btCancelar.Click
        MultiView1.ActiveViewIndex = 1
    End Sub

    Protected Sub gvConsulta_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles gvConsulta.RowCommand
        Select Case e.CommandName

            Case "Editar"
                Dim index As Integer = Convert.ToInt32(e.CommandArgument)
                Me.gvConsulta.SelectedIndex = index
                Dim obj As New Personas
                Dim dt As Data.DataTable = obj.GetbyPK(gvConsulta.DataKeys(index).Values(0).ToString())
                If dt.Rows.Count > 0 Then
                    Me.TxtCed.Text = dt.Rows(0)("Cedula").ToString
                    Me.TxtNom1.Text = dt.Rows(0)("Primer_Nombre").ToString
                    Me.TxtNom2.Text = dt.Rows(0)("Segundo_Nombre").ToString
                    Me.TxtApe1.Text = dt.Rows(0)("Primer_Apellido").ToString
                    Me.TxtApe2.Text = dt.Rows(0)("Segundo_Apellido").ToString
                    Me.TxtDir.Text = dt.Rows(0)("Direccion").ToString
                    Me.TxtTel.Text = dt.Rows(0)("Telefono").ToString
                    Me.TxtCel.Text = dt.Rows(0)("Celular").ToString
                    Me.TxtEma.Text = dt.Rows(0)("Email").ToString
                    Me.CmbSexo.SelectedValue = dt.Rows(0)("Sexo").ToString
                    Me.CmbTipDoc.SelectedValue = dt.Rows(0)("Tip_Doc").ToString
                    If dt.Rows(0)("Fecha_Nacimiento").ToString <> "" Then
                        Me.DtpFecNac.Text = CDate(dt.Rows(0)("Fecha_Nacimiento").ToString).ToShortDateString
                    Else
                        Me.DtpFecNac.Text = Today
                    End If
                    lbEstado.Text = "Actualizando..."
                    hfPk1.Value = gvConsulta.SelectedValue
                    hfTransaccion.Value = "U"
                    MultiView1.ActiveViewIndex = 0
                End If
        End Select
    End Sub
    Protected Overridable Sub Onclick(ByVal sender As Object)
        RaiseEvent SelClicked(sender, New EventArgs)
    End Sub

    Protected Sub gvConsulta_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles gvConsulta.SelectedIndexChanged
        Onclick(sender)
    End Sub
    Sub limpiar()
        Me.TxtCed.Text = ""
        Me.TxtNom1.Text = ""
        Me.TxtNom2.Text = ""
        Me.TxtApe1.Text = ""
        Me.TxtApe2.Text = ""
        Me.TxtDir.Text = ""
        Me.TxtTel.Text = ""
        Me.TxtCel.Text = ""
        Me.TxtEma.Text = ""
        Me.DtpFecNac.Text = ""
        MsgBoxLimpiar(Me.lbEstado)
    End Sub
End Class
